# 理解： https://juejin.cn/post/7436662001802412084
def solution(stones: list) -> int:
    # PLEASE DO NOT MODIFY THE FUNCTION SIGNATURE
    # write code here
    if len(stones) == 1:
        return 0

    stones = sorted(stones)
    gaps = []
    # 获取相邻元素的间隔
    for i in range(1, len(stones)):
        gaps.append(stones[i] - stones[i - 1] - 1)

    if gaps[0] > gaps[-1]:
        return sum(gaps[:-1])
    else:
        return sum(gaps[1:])


if __name__ == '__main__':
    print(solution(stones=[7, 4, 9]) == 2)
    print(solution(stones=[6, 5, 4, 3, 10]) == 3)
    print(solution(stones=[1, 2, 3, 4, 5]) == 0)
